package com.zrf.service;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.zrf.dao.impl.CheckDaoImpl;
import com.zrf.dao.impl.ManifestDaoImpl;
import com.zrf.dao.impl.ProductDaoImpl;
import com.zrf.dao.impl.StorageDaoImpl;
import com.zrf.model.Storage;
import com.zrf.pojo.Product;

@Service("productService")
public class ProductService {
	@Autowired
	private StorageDaoImpl sdi;
	@Autowired
	private ProductDaoImpl pdi;
	@Autowired
	private ManifestDaoImpl ldi;
	@Autowired
	private CheckDaoImpl cdi;
	
	public List<?> getAllProduct(int sid) {
		return pdi.findAllBySid(sid);
	}
	
	/**仓库剩余库存*/
	public Storage getStorageById(int id) {
		return sdi.getStorageById(id);
	}

	public List<?> findAll() {
		// TODO Auto-generated method stub

		try {
			return sdi.findAll();
		} catch (Exception e) {
			// TODO Auto-generated catch block

			e.printStackTrace();
		}
		return null;
	}

	public void deleteByPid(int pid) {
		// TODO Auto-generated method stub

		try {
			pdi.deleteById(pid);
		} catch (Exception e) {
			// TODO Auto-generated catch block

			e.printStackTrace();
		}
	}

	public void addOrUpdate(Product pro) {
		// TODO Auto-generated method stub

		Product p = new Product();
		p = pdi.findForObject(pro.getPname(),pro.getStorage().getSid());
		try {
			if (p==null) {
				pdi.add(pro);
			}else {
				Double pprice = (pro.getPprice()*pro.getPnum()+p.getPprice()*p.getPnum());
				p.setPnum(p.getPnum()+pro.getPnum());
				p.setPprice(pprice/p.getPnum());
				pdi.update(p);
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block

			e.printStackTrace();
		}
	}
	
}